package net.gyapp.meiriyiwenpro2;

import android.os.Environment;
import android.preference.Preference;
import android.util.Log;

import java.io.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.*;

import static android.os.Environment.DIRECTORY_DOCUMENTS;

/**
 * Created by lbc on 2016/11/21.
 */
public class ArticleUtils {
    public static final String TAG = "ArticleUtils";
    private static File getArticleDirectory(){
        File doc = Environment.getExternalStorageDirectory(); //Environment.getExternalStoragePublicDirectory(DIRECTORY_DOCUMENTS);
        File artcleDirectory = new File(doc,"MeiriyiwenArticles");
        if (!artcleDirectory.exists())
            artcleDirectory.mkdirs();
        Log.i(TAG,doc + "=doc");
        Log.i(TAG,artcleDirectory + "=artcleDirectory");
        return artcleDirectory;
    }
    public static List<String> getAllReadRecords(){
        List<String> results = new ArrayList<String>();
        File file = new File(getArticleDirectory(),"readRecords.txt");
        if (!file.exists()){
            return results;
        }
        BufferedReader buff = null;
        try {
            buff = new BufferedReader(new FileReader(file));
            //buff.
            String s = null;
            while ((s = buff.readLine()) != null){
                results.add(s);
                Log.i(TAG,s);
            }
            return results;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (buff != null){
                try {
                    buff.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return results;
    }
    public static void insertOneReadRecord(String author,String title){
        //Preference preference =
        File file = new File(getArticleDirectory(),"readRecords.txt");
        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        BufferedWriter buff = null;
        try {
            buff = new BufferedWriter(new FileWriter(file,true));
            buff.write(author + "_" + title);
            buff.newLine();
            buff.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (buff != null){
                try {
                    buff.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static boolean saveArticle(String author,String title,String content){
        //Environment.getExternalStoragePublicDirectory(DIRECTORY_DOCUMENTS);
        //FileOutputStream fileOutputStream = null;
        ZipOutputStream zipOutputStream = null;
        try {
            File file = new File(getArticleDirectory(),author + "_" + title +".zip");
            if (file.exists()){
                //文件已经存在
                return false;
            }
            //zipFile.get
            Log.i(TAG,file + "=file");

            file.createNewFile();

            //加入浏览记录
            insertOneReadRecord(author,title);

            //ZipFile zipFile = new ZipFile(file);

            zipOutputStream = new ZipOutputStream(new FileOutputStream(file));

            zipOutputStream.putNextEntry(new ZipEntry(file.getName().replace(".zip","")));

            zipOutputStream.write(content.getBytes("UTF-8"));

            zipOutputStream.flush();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zipOutputStream != null){
                try {
                    zipOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }

    /**
     * 读取文章
     * @param author    作者
     * @param title     标题
     * @return          文章内容，可能为空
     */
    public static String readAirticle(String author, String title) {

        return readAirticle(author + "_" + title +".zip");
    }
    public static String readAirticle(String fileName){
        StringBuffer stringBuffer = new StringBuffer();
        try {

            File file = new File(getArticleDirectory(),fileName);

            ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file));

            //byte [] tmps = new byte[256];
            //zipInputStream.read(tmps);
            ZipFile zipFile = new ZipFile(file);
            //ByteBuffer
            ZipEntry zipEntry = null;

            while ((zipEntry = zipInputStream.getNextEntry()) != null){
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
                // BufferedInputStream bufferedInputStream = new BufferedInputStream(
//                zipFile.getInputStream(zipEntry));
                byte [] temp = new byte[256];
                long len = 0;
                String s = null;
                //By
                //生成临时文件
                // while ((len = bufferedInputStream.read(temp)) != -1){
                //stringBuffer.a
                while ((s = bufferedReader.readLine()) != null){
                    stringBuffer.append(s);
                }
            }
            return stringBuffer.toString();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ZipException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }
}
